﻿using SchoolCommunicationSystem.Models;
using System;
using System.Linq;
using System.Web.Mvc;

namespace SchoolCommunicationSystem.Controllers
{
    public class TemplatesController : Controller
    {
        TemplatesModels tm;

        /// <summary>
        /// TemplatesController
        /// </summary>
        public TemplatesController()
        {
            tm = new TemplatesModels(); 
        }

        /// <summary>
        /// Pre DownloadTemplates
        /// </summary>
        /// <returns></returns>
        public ActionResult DownloadTemplates()
        {
            var files = tm.GetFiles();  
            return View(files);
        }

        /// <summary>
        /// DownloadTemplates
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>FileList</returns>
        public FileResult Download(string id)
        {
            int fid = Convert.ToInt32(id);
            var files = tm.GetFiles();
            string filePath = (from f in files
                               where f.FileId == fid
                               select f.FilePath).First();
            string contentType = "application/xls&docx";
            string fileName = (from f in files
                               where f.FileId == fid
                               select f.FileName).First();
            return File(filePath, contentType, fileName);
        }

    }
}
